﻿using System;
using System.Collections.Generic;

namespace Memento.Game
{
    internal class Gamer
    {
        private int money;
        private List<string> fruits;
        private Random random;

        private static string[] fruitsName ={
                                                "リンゴ",
                                                "ぶどう",
                                                "バナナ",
                                                "みかん",
                                            };

        public Gamer(int money)
        {
            this.money = money;
            this.fruits = new List<string>();
            this.random = new Random();
        }

        public int GetMoney()
        {
            return money;
        }

        public void Bet()
        {
            int dice = random.Next(6) + 1;

            if (dice == 1)
            {
                money += 100;
                Console.WriteLine("所持金が増えました。");
            }
            else if (dice == 2)
            {
                money /= 2;
                Console.WriteLine("所持金が半分になりました。");
            }
            else if (dice == 6)
            {
                string f = GetFruit();
                Console.WriteLine("フルーツ(" + f + ")を貰いました。");
            }
            else
            {
                Console.WriteLine("何も起こりませんでした。");
            }
        }

        public Memento CreateMemento()
        {
            Memento m = new Memento(money);

            foreach (string f in fruits)
            {
                if (f.StartsWith("おいしい"))
                {
                    m.AddFruit(f);
                }
            }

            return m;
        }

        public void RestoreMemento(Memento memento)
        {
            this.money = memento.GetMoney();
            this.fruits = memento.GetFruits();
        }

        public override string ToString()
        {
            return "[money = " + money + ", fruits = " + fruits + "]";
        }


        private string GetFruit()
        {
            string prefix = "";

            if (random.Next() % 2 == 0)
            {
                prefix = "おいしい";
            }

            return prefix + fruitsName[random.Next(fruitsName.Length)];
        }
    }
}
